﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace TestPages
{
    //add UserContols's styles and scripts to page 
    public class BasePage : Page
    {
        private List<string> styles = new List<string>();
        private List<string> scripts = new List<string>();

        //add file css to list
        public void AddStyle(string stylePath)
        {
            if (!styles.Contains(stylePath))       
            {
                styles.Add(stylePath);
            }
        }
        
        //format of CSS link
        private HtmlLink CreateStyle(string path)
        {
            var link = new HtmlLink { Href = path };

            link.Attributes["type"] = "text/css";
            link.Attributes["rel"] = "stylesheet";

            return link;
        }


        private void InitStyles()
        {
            foreach (var style in styles)
            {
                Header.Controls.Add(CreateStyle(style));
            }
        }

        //in this event were added files of css and scripts
        protected void Page_Init(object sender, EventArgs e)
        {
            InitScript();
            InitStyles();
        }

        private void InitScript()
        {
            foreach (var script in scripts)
            {
                Page.ClientScript.RegisterClientScriptInclude("ScriptOfUserControl.js", script);
            }
        }

        public void AddScript(string scriptPath)
        {
            if (!scripts.Contains(scriptPath))      
            {
                scripts.Add(scriptPath);
            }
        }
    }
}